﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using bdnt_pets_system.Tools;
using System.Collections.ObjectModel;
using bdnt_pets_system_Entity;
using bdnt_pets_system.FailureServiceReference;
using bdnt_pets_system.Resources;
using bdnt_pets_system.DataServiceReference;
using bdnt_pets_system.MyUserControls;
using System.Windows.Data;

namespace bdnt_pets_system.PageViews.MatePart
{
    public partial class MatePartManagerControl : BaseUserControl
    {
        DataServiceReference.DataServiceClient dClient;
        FailureServiceReference.FailureServiceClient fClient;
        int pageSize = SessionManager.PageSize;
        int currentPage = 1;

        public MatePartManagerControl()
        {
            InitializeComponent();
            dClient = new DataServiceClient();
            fClient = new FailureServiceReference.FailureServiceClient();
            BingVehicleData();
            InitEvent();
            QueryData();
        }

        void InitEvent()
        {
            btnSelect.Click += new RoutedEventHandler(btnSelect_Click);
            fClient.GetMatePartListByConditionCountCompleted += new EventHandler<FailureServiceReference.GetMatePartListByConditionCountCompletedEventArgs>(fClient_GetMatePartListByConditionCountCompleted);
            fClient.GetMatePartListByConditionCompleted += new EventHandler<FailureServiceReference.GetMatePartListByConditionCompletedEventArgs>(fClient_GetMatePartListByConditionCompleted);
            MyPageControl.dpPage.PageIndexChanged += new EventHandler<EventArgs>(dpPage_PageIndexChanged);
            fClient.GetMatePartStationListByConditonCompleted += new EventHandler<FailureServiceReference.GetMatePartStationListByConditonCompletedEventArgs>(fClient_GetMatePartStationListByConditonCompleted);
            fClient.DeleteMatePartCompleted += new EventHandler<DeleteMatePartCompletedEventArgs>(fClient_DeleteMatePartCompleted);

            #region 扫描部件
            dClient.GetVehicleByProNoOrVinNoCompleted += new EventHandler<DataServiceReference.GetVehicleByProNoOrVinNoCompletedEventArgs>(dClient_GetVehicleByProNoOrVinNoCompleted);
            tbVINNO.KeyDown += new KeyEventHandler(tbVINNO_KeyDown);
            tbBarNo.KeyDown += new KeyEventHandler(tbBarNo_KeyDown);
            fClient.SaveOneMatePartCompleted += new EventHandler<SaveOneMatePartCompletedEventArgs>(fClient_SaveOneMatePartCompleted);
            #endregion
        }

        #region 查询扫描
        void btnSelect_Click(object sender, RoutedEventArgs e)
        {
            QueryData();
        }

        string selectVinNo = "";
        void QueryData()
        {
            selectVinNo = tbSelectVINNo.Text;
            fClient.GetMatePartListByConditionCountAsync(selectVinNo,SessionManager.CurrentUser.StationIds);
        }

        void fClient_GetMatePartListByConditionCountCompleted(object sender, FailureServiceReference.GetMatePartListByConditionCountCompletedEventArgs e)
        {
            MyPageControl.LoadPages(e.Result, pageSize);
        }

        void dpPage_PageIndexChanged(object sender, EventArgs e)
        {
            currentPage = MyPageControl.PageIndex + 1;
            fClient.GetMatePartListByConditionAsync(selectVinNo, pageSize, currentPage, SessionManager.CurrentUser.StationIds);
        }

        void fClient_GetMatePartListByConditionCompleted(object sender, FailureServiceReference.GetMatePartListByConditionCompletedEventArgs e)
        {
            gdList.ItemsSource = e.Result;
        }

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show(MyStrings.qdyscm, MyStrings.ts, MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                return;
            var cuo = ((sender as HyperlinkButton).Tag as Tbl_MatePart);
            fClient.DeleteMatePartAsync(cuo.Id);
        }

        void fClient_DeleteMatePartCompleted(object sender, DeleteMatePartCompletedEventArgs e)
        {
            if (e.Result)
            {
                QueryData();
                if (CuTbl_Vehicle == null)
                    return;
                dClient.GetVehicleByProNoOrVinNoAsync("", CuTbl_Vehicle.VINNo);
            }
        }

        #endregion

        #region 扫描部件
        void BingVehicleData()
        {
            tbProducitonNo.SetBinding(TextBlock.TextProperty, new Binding("ProducitonNo"));
            tbVPhase.SetBinding(TextBlock.TextProperty, new Binding("VPhase"));
            tbVTYPE.SetBinding(TextBlock.TextProperty, new Binding("VType"));
            tbVColor.SetBinding(TextBlock.TextProperty, new Binding("VColor"));
            tbVNsColor.SetBinding(TextBlock.TextProperty, new Binding("InteriorColor"));
            tbRemark.SetBinding(TextBlock.TextProperty, new Binding("Remark"));
            if (SessionManager.IsCN_ZH)
                tbVConfiguration.SetBinding(TextBlock.TextProperty, new Binding("VConfigurationName"));
            else
                tbVConfiguration.SetBinding(TextBlock.TextProperty, new Binding("en_VConfigurationName"));
        }

        public Tbl_Vehicle CuTbl_Vehicle
        {
            get { return gdVehicle.DataContext as Tbl_Vehicle; }
            set { gdVehicle.DataContext = value; }
        }

        Tbl_MatePart CuTbl_MatePart;
        ObservableCollection<Tbl_MatePart> mateParts;

        void fClient_SaveOneMatePartCompleted(object sender, SaveOneMatePartCompletedEventArgs e)
        {
            if (!e.Result)
                errorMsg.Text = "保存失败";
            else QueryData();
        }

        void tbVINNO_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (string.IsNullOrEmpty(tbVINNO.Text))
                {
                    //errorMsg.Text = "空VINNO";
                    return;
                }
                errorMsg.Text = "";
                dClient.GetVehicleByProNoOrVinNoAsync("", tbVINNO.Text);
            }
        }

        void dClient_GetVehicleByProNoOrVinNoCompleted(object sender, DataServiceReference.GetVehicleByProNoOrVinNoCompletedEventArgs e)
        {
            CuTbl_Vehicle = e.Result;
            if (CuTbl_Vehicle == null)
                errorMsg.Text = "数据库中不存在此车辆信息";
            else
            {
                fClient.GetMatePartStationListByConditonAsync(SessionManager.CurrentUser.StationIds, tbVINNO.Text);
            }
        }

        void fClient_GetMatePartStationListByConditonCompleted(object sender, FailureServiceReference.GetMatePartStationListByConditonCompletedEventArgs e)
        {
            mateParts = e.Result;
            spBarNo.Children.Clear();
            foreach (var item in mateParts)
            {
                MyMatePart mmp = new MyMatePart();
                mmp.BindData(item);
                spBarNo.Children.Add(mmp);
            }
            tbBarNo.Focus();
        }

        void tbBarNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (CuTbl_Vehicle == null)
                {
                    errorMsg.Text = "请先扫描VINNO";
                    return;
                }
                errorMsg.Text = "";
                if (string.IsNullOrEmpty(tbBarNo.Text))
                {
                    //errorMsg.Text = "空条形码";
                    return;
                }
                errorMsg.Text = "";
                if (!ValidateData())
                    return;
                fClient.SaveOneMatePartAsync(CuTbl_MatePart);
            }
        }

        bool ValidateData()
        {
            if (mateParts == null || mateParts.Count < 1)
            {
                errorMsg.Text = "当前用户没有任何需要扫描的部件";
                //MessageBox.Show("当前用户没有任何需要扫描的部件", "提示", MessageBoxButton.OK);
                return false;
            }

            if (CuTbl_Vehicle == null)
            {
                errorMsg.Text = "请扫描VINNO";
                //MessageBox.Show("当前用户没有任何需要扫描的部件", "提示", MessageBoxButton.OK);
                return false;
            }
            bool re = true;

            string cubov = tbBarNo.Text;
            if (cubov.IndexOf("_") < 0)
            {
                re = false;
                errorMsg.Text = "条形码格式不对";
                //MessageBox.Show("条形码格式不对", "提示", MessageBoxButton.OK);
                return re;
            }
            var pLs = cubov.Split('_');
            CuTbl_MatePart = mateParts.FirstOrDefault(p => p.PARTNO == pLs[0]);
            if (CuTbl_MatePart == null)
            {
                re = false;
                errorMsg.Text = "当前用户没有权限扫描此条形码";
                //MessageBox.Show("当前用户没有权限扫描此条形码", "提示", MessageBoxButton.OK);
                return re;
            }
            if (CuTbl_MatePart.IsSMed)
            {
                re = false;
                errorMsg.Text = "已扫描";
                //MessageBox.Show("当前用户没有权限扫描此条形码", "提示", MessageBoxButton.OK);
                return re;
            }


            if (CuTbl_Vehicle.VConfiguration != pLs[1])
            {
                re = false;
                errorMsg.Text = "部件配置与车辆配置不一致";
                //MessageBox.Show("当前用户没有权限扫描此条形码", "提示", MessageBoxButton.OK);
                return re;
            }

            CuTbl_MatePart.BARNO = cubov;
            CuTbl_MatePart.CREATETIME = DateTime.Now;
            CuTbl_MatePart.CREATEUSERID = SessionManager.CurrentUser.Id;
            CuTbl_MatePart.VINNO = CuTbl_Vehicle.VINNo;
            CuTbl_MatePart.IsSMed = true;
            return re;
        }
        #endregion
    }
}
